package cn.piesat.scanning.business.xz.service.ecqimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildWSFIServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //ECQ-WSFI防风固沙指数计算
        Map<String, Object> params = new HashMap<>();
        String fileName = primaryFile.substring(primaryFile.lastIndexOf("/") + 1);
        String[] s1 = fileName.split("_");
        String level = s1[3];
        String satellite = s1[4];
        String region = s1[2];
        String endTime = s1[9];
        Date date = DateUtil.parse(endTime, "yyyyMMddHHmmss");
        Date startTime = DateUtil.offsetMonth(date, -60);
        String dateTimeStr = DateUtil.format(startTime, "yyyyMMddHHmmss");
        List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(dateTimeStr, endTime, level, satellite,
                "NPP", region, "POAY", "FOR", "TIFF");

        List<String> collect = metadataDTOList.stream().map(x -> {
            if (x.getFileDate().equals(endTime)) {
                params.put("nppPOAYFile", taskPathParam.getParameter02() + File.separator + x.getFilePath());
            }
            return taskPathParam.getParameter02() + File.separator + x.getFilePath();
        }).collect(Collectors.toList());
        String join = StrUtil.join(",", collect);
        if (metadataDTOList.size() < 2 || StringUtils.isEmpty(params.get("nppPOAYFile").toString())) {
            algorithmParams.put("parameter01", null);
        } else {
            params.put("ndviMOAYFile", primaryFile);
            params.put("nppPO5YFile", join);
            params.put("satelliteName", satellite);
            params.put("auxPath", taskPathParam.getParameter03());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
